package com.yiren.dbaa.module.practice_set.carmodel.presenter;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.backstage.currency.entity.CarModelEntity;
import com.yiren.backstage.currency.entity.LineInfoEntity;
import com.yiren.backstage.db.dao.controller.GradeDbController;
import com.yiren.backstage.db.dao.controller.UserDbController;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.currency.dialog.CommonDialog;
import com.yiren.dbaa.module.practice_set.PracticeCarSetActivity;
import com.yiren.dbaa.module.practice_set.carmodel.contract.CarModelHistoryContract;
import com.yiren.dbaa.module.practice_set.carmodel.contract.CarModelHomeContract;
import com.yiren.dbaa.module.practice_set.carmodel.contract.CarModelReleaseContract;
import com.yiren.dbaa.module.practice_set.carmodel.model.CarModelHistoryModel;
import com.yiren.dbaa.module.practice_set.carmodel.model.CarModelHomeModel;
import com.yiren.dbaa.module.practice_set.carmodel.model.CarModelReleaseModel;
import com.yiren.dbaa.module.practice_set.carmodel.view.activity.CarModelCollectActivity;
import com.yiren.dbaa.module.practice_set.line.view.activity.LineEditActivity;
import com.yiren.dbaa.view.dialog.LoadDialog;
import com.yiren.dbaa.view.toast.MessageBox;

import java.util.List;

/**
 * @author lwb
 * @date : 2022/12/6 17:17
 * @description: 车模历史版本
 */
public class CarModelHistoryPresenter implements CarModelHistoryContract.Presenter {

    private Activity activity;
    private UserParamConfigBean userParamConfigBean;
    private CarModelHistoryContract.Model versionHistoryModel;
    private CarModelReleaseContract.Model releaseModel;
    private CarModelHomeContract.Model homeModel;

    public CarModelHistoryPresenter(Activity activity) {
        this.activity = activity;
        versionHistoryModel = new CarModelHistoryModel(activity);
        releaseModel = new CarModelReleaseModel(activity);
        homeModel = new CarModelHomeModel(activity);
        userParamConfigBean = LoadConfigApi.loadUserParamConfig(activity);
    }

    @Override
    public void loadVersionList(CarModelHistoryContract.View view, String userId, CarModelEntity carModel) {
        LoadDialog.show(activity, "正在加载数据...");
        versionHistoryModel.getVersionList(carModel, new CommonCallBack() {
            @Override
            public void success(Object result) {
                LoadDialog.dismiss(activity);
                List<CarModelEntity> carList = (List<CarModelEntity>) result;
                view.showVersionList(carList);
            }

            @Override
            public void fail(String error) {
                LoadDialog.dismiss(activity);
                MessageBox.show(activity, error);
            }
        });
    }

    @Override
    public void downLoadCarModelData(CarModelEntity carModel, CarModelHistoryContract.View view) {
        homeModel.downLoadCarModelPoint(carModel, new CommonCallBack() {
            @Override
            public void success(Object result) {
                List<CarModelEntity> carModelList = (List<CarModelEntity>) result;
                for (CarModelEntity carModelEntity : carModelList) {
                    if (UserDbController.getInstance(false).insertOrReplace(carModelEntity)) {
                        //通知视图
                        LoadDialog.dismiss(activity);
                        MessageBox.MessageAndVoiceModel(activity, "下载成功");
                        CommonDialog.showTwoBtn(activity, "操作确认", "下载已完成，请问是直接发布，还是编辑车模？", "直接发布", "编辑车模", (dialog, which) -> {
                            if (which == -2) {
                                releaseCarModel(carModel);
                            } else {
                                activity.finish();
                                Intent intent = new Intent(activity, CarModelCollectActivity.class);
                                intent.putExtra("carModel", carModel);
                                activity.startActivity(intent);
                            }
                            dialog.dismiss();
                        });
                    } else {
                        MessageBox.MessageAndVoiceModel(activity, "下载失败");
                    }
                }
            }

            @Override
            public void fail(String error) {
                MessageBox.MessageAndVoiceModel(activity, "下载失败");
            }
        });
    }

    @Override
    public void releaseCarModel(CarModelEntity carModel) {
        releaseModel.publishCarModel(carModel, userParamConfigBean.getUserInfo().getId().toString(), carModel.getUpdateDetails(), carModel.getUpdateMode(), new CommonCallBack() {
            @Override
            public void success(Object result) {
                activity.runOnUiThread(() -> {
                    carModel.setCarModelVer(carModel.getCarModelVerLast() + 1);
                    UserDbController.getInstance(false).update(carModel);
                    LoadDialog.dismiss(activity);
                    MessageBox.MessageAndVoiceModel(activity, "发布成功!");
                    activity.startActivity(new Intent(activity, PracticeCarSetActivity.class));
                });
            }

            @Override
            public void fail(String error) {
                activity.runOnUiThread(() -> CommonDialog.showOneBtn(activity, "操作提示", "数据发布失败!", "我知道了", (dialog, which) -> {
                    dialog.dismiss();
                }));
            }
        });
    }

}
